package com.sky.controller.user;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.entity.UserStatus;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserLoginService;
import com.sky.service.UserStatusService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 符健清
 * @version 1.0
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping ("/user/user")
@Slf4j
@Api (tags = "用户端登入接口")
 public class UserController {
    @Autowired
    private UserStatusService userStatusService;
    @Autowired
    private JwtProperties  jwtProperties;
    @Autowired
    private UserLoginService userLoginService;
    @ApiOperation("登入接口")
    @PostMapping("/login")
    public Result<UserLoginVO>  userLogin ( @RequestBody UserLoginDTO  userLoginDTO) {
        log.info("userloginDto:{}",userLoginDTO);
//       微信登入
        User user = userLoginService.login(userLoginDTO);
//       实用工具jjwt 令牌工具生成令牌
        Map<String , Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token= JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
//      UserLoginVO 封装
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setId(user.getId());
        userLoginVO.setOpenid(user.getOpenid());
        userLoginVO.setToken(token);
            Result<UserLoginVO> userLoginVOResult = new Result<>();
        return userLoginVOResult.success(userLoginVO);
    }

    /**
     * 用户退出
     */
    //TODO 待完善
    @ApiOperation("用户退出")
    @PostMapping("/logout")
    public Result  userExit  (@RequestBody UserStatus userStatus) {
         userStatusService.updateUserStatus(userStatus);
        Result<Object> result = new Result<>();
        return result.success();
    }




}
